# coding=utf-8
import json

import pandas
import redis


class MyRedis2(object):

    def __init__(self, ip='127.0.0.1'):
        self.r = redis.Redis(host=ip, port=6379, db=0)

    def set(self, key, data):
        self.r.set(key, data)

    def get(self, key):
        self.r.get(key)

    def set_dict(self, key, dict_data):
        df = pandas.DataFrame(dict_data)
        for c in df.columns.tolist():
            df[c] = df[c].apply(lambda x: str(x))
        self.r.set(key, df.to_json(orient='records'))

    def get_dict(self, key):
        rd = self.r.get(key)
        return json.loads(rd) if rd else []


class MyRedis(object):

    def __init__(self):
        self.r = {}

    def set(self, key, data):
        self.r.__setitem__(key, data)

    def get(self, key):
        self.r.get(key)

    def set_dict(self, key, dict_data):
        df = pandas.DataFrame(dict_data)
        for c in df.columns.tolist():
            df[c] = df[c].apply(lambda x: str(x))
        self.r.__setitem__(key, df.to_json(orient='records'))

    def get_dict(self, key):
        rd = self.r.get(key)
        return json.loads(rd) if rd else []


mr = MyRedis()
